<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2017/12/2
 * Time: 16:57
 */
namespace app\admin\controller;

use app\admin\Controller;
use think\Session;
use think\Db;

class Order extends Controller
{

    /**
     * 订单列表
     */
    public function index(){

        $params = $this->request->param();
        $maps = [];
        if(isset($params['uid'])&&$params['uid']!=''){
            $maps['uid'] = array('eq',intval($params['uid']));
        }

        if(isset($params['order_id'])&&$params['order_id']!=''){
            $maps['order_id'] = array('eq',intval($params['order_id']));
        }

        if(isset($params['status']) && $params['status']!=''){
            $maps['status'] = array('eq',intval($params['status']));
        }

        if(isset($params['pay_type']) && $params['pay_type']!=''){
            $maps['pay_type'] = array('eq',intval($params['pay_type']));
        }

        // 分页查询 每页显示10条
        $goods = Db::table('kyf_order')->where($maps)->order('order_id desc')->paginate(10,false,$this->request->param());
        $res = $goods->toArray();
        $pages = $goods->render();

        foreach ($res['data'] as &$val){
            $val['total_money'] = fmoney($val['total_money'],false);
            $val['youhui_money'] = fmoney($val['youhui_money'],false);
            $val['postfee'] = fmoney($val['postfee'],false);
            $val['real_money'] = fmoney($val['real_money'],false);
            $val['diff_money'] = fmoney($val['diff_money'],false);
        }


        $this->view->assign('list',$res['data']);
        $this->view->assign('total',$res['total']);
        $this->view->assign('page',$pages);

        return $this->view->fetch();
    }


    /**
     * 订单详情
     * @return string
     */
    public function show(){
        $params = $this->request->param();
        $id = isset($params['id'])?intval($params['id']):0;
        if($id==0) exit('参数错误');
        $info = Db::table('kyf_order')->where('order_id='.$id)->find();
        if(empty($info)) exit('订单信息错误');


        $info['total_money'] = fmoney($info['total_money'],false);
        $info['youhui_money'] = fmoney($info['youhui_money'],false);
        $info['postfee'] = fmoney($info['postfee'],false);
        $info['real_money'] = fmoney($info['real_money'],false);
        $info['diff_money'] = fmoney($info['diff_money'],false);


        // 购物清单
        $cartidstr = trim($info['cart_idstr'],',');
        $maps['cart_id'] = array('in',$cartidstr);

        $list = Db::table('kyf_shopping_cart')->where($maps)->select();

        foreach ($list as &$val){
            $goods = Db::table('kyf_goods')->where('goods_id=' . $val['goods_id'])->find();
            $picarr = explode(',',$goods['goods_pics']);
            $val['goods_pic'] = $picarr[0];
            $val['buy_price'] = fmoney($goods['price'],false);
        }
        $this->view->assign('cartlist',$list);


        //收货地址
        $address = array(
            'name'=>'冯先生',
            'mobile'=>'18513333302',
            'addr'=>' 北京 昌平 回龙观地区 马连道村',
        );
        $this->view->assign('address',$address);


        $this->view->assign('info',$info);
        return $this->view->fetch();
    }


    /**
     * 备注补差价
     */
    public function mark(){
        $params = $this->request->param();
        $id = isset($params['id'])?intval($params['id']):0;
        if($id==0) echo json_encode(array('code'=>1,'msg'=>'参数错误'));

        if($this->request->isPost()) {
            $maps['order_id'] = array('eq', $id);
            $diff_money = isset($params['diff_money'])?trim($params['diff_money']):'';
            if($diff_money=='') echo json_encode(array('code'=>2,'msg'=>'参数错误'));
            $srt = Db::table('kyf_order')->where($maps)->update(array('diff_money'=>fmoney($diff_money)));
            if($srt){
                return ajax_return(array(),'操作成功');
            } else {
                return ajax_return(array(),'操作失败',1);
            }
        }

        $this->view->assign('oid',$id);
        return $this->view->fetch();
    }

}